package us.versus.them.weeds.constraints;

import flash.display.Graphics;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.Lib;

import us.versus.them.weeds.Vertlet;

class Offset implements Constraint {
	var start:Vertlet;
	var stop:Vertlet;

	var start_mass_ratio:Float;
	var stop_mass_ratio:Float;

	var diff:Point;

	public function new( start:Vertlet, stop:Vertlet ) {
		this.start = start;
		this.stop = stop;
		this.diff = new Point(
			this.stop.current.x - this.start.current.x 
			, this.stop.current.y - this.start.current.y 
		);

		var mass = start.mass + stop.mass;
		this.start_mass_ratio = start.mass / mass;
		this.stop_mass_ratio  = 1 - this.start_mass_ratio;
	}

	public function enforce() {

		var mid_point_x = this.start.current.x + this.stop_mass_ratio * (
			this.stop.current.x - this.start.current.x
		);
		var mid_point_y = this.start.current.y + this.stop_mass_ratio * (
			this.stop.current.y - this.start.current.y
		);

		this.start.current.x = mid_point_x - this.diff.x * this.stop_mass_ratio;
		this.start.current.y = mid_point_y - this.diff.y * this.stop_mass_ratio;

		this.stop.current.x = mid_point_x + this.diff.x * this.start_mass_ratio;
		this.stop.current.y = mid_point_y + this.diff.y * this.start_mass_ratio;
	}
}
